// ignore_for_file: dangling_library_doc_comments, slash_for_doc_comments

/*** 
 * @Author: Zhong
 * @Date: 2025-03-21 13:58:28
 * @LastEditors: Zhong zhongxiaodong@eenet.com
 * @LastEditTime: 2025-07-16 16:24:54
 * @FilePath: /wan_android_flutter/lib/module_code/login/api/api_login.dart
 * @Description: 登录模块接口
 */

import 'package:flutter/widgets.dart';
import 'package:dio/dio.dart';
import 'package:wan_android_flutter/common_code/models/base_model.dart';

import '../../../common_code/wrapper/dio_wrapper/dio_client.dart';
import '../models/login_model.dart';

class ApiLogin {
  //注册接口
  static Future<bool> register({
    required String username,
    required String password,
    required String rePassword,
  }) async {
    // Call API
    FormData data = FormData.fromMap({
      'username': username,
      'password': password,
      'repassword': rePassword,
    });
    var response = await DioClient.post('/user/register', data: data);
    BaseModel baseModel = BaseModel.fromJson(response.data);
    if (baseModel.errorCode != 0) {
      return false;
    }
    return true;
  }

  //登录接口
  static Future<bool> login({
    required String username,
    required String password,
  }) async {
    // Call API
    FormData data = FormData.fromMap({
      'username': username,
      'password': password,
    });
    var response = await DioClient.post(
      '/user/login',
      data: data,
      onCacheData: (cacheData) {
        debugPrint('onCacheData: $cacheData');
      },
    );
    LoginModel loginModel = LoginModel.fromJson(response.data);
    if (loginModel.errorCode == 0) {
      //登录成功
      //缓存登录数据
      loginModel.data?.save();
      return true;
    }
    return false;
    //获取用户信息
    // getUserInfo();
  }

  //获取用户信息接口
  static void getUserInfo() async {
    // Call API
    var response = await DioClient.post(
      '/api/oauth/appUser/getUserInfo',
      data: {},
    );
    debugPrint(response.data);
  }
}
